<?php

namespace App\Store\Models\Financial\Statements;

use App\Common\Services\Common;
use App\Store\Models\Basic\PaytypeList;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use App\Common\Services\Store\Common as StoreCommon;

class Moneylog extends Model
{
    protected $table = 'site_financial_statements_moneylog';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];

    /**
     *获取列表
     */
    public static function getList(Request $request)
    {
        $cp_id = StoreCommon::getCurrentAccountCompanyId($request);
        $query = self::with(['paytypeinfo','subjectinfo'])->where('is_del', 1)->where('cp_id',$cp_id)->orderBy('create_at', 'asc')->orderBy('id', 'asc');


        $store_ids = StoreCommon::getCurrentAccountStoreId($request);

        if($store_ids){//权限控制
            if(is_array($store_ids)){//获取当前权限下的门店
                $query->whereIn('store_id',$store_ids);
            }
            if(is_numeric($store_ids) && intval($store_ids)>0){//获取当前门店下的列表
                $query->where('store_id', intval($store_ids));
            }
        }

        $store_id = $request->store_id;//下拉单选搜索
        if ($store_id>0) {
            $query->where('store_id', $store_id);
        }

        //订单状态
        $type = $request['type'] ?? 0;
        if ($type != 0) {
            $query->where('type', $type);
        }

        $log_date = $request['log_date'] ?? '';
        if ($log_date != '') {
            $query->where('log_date', $log_date);
        }
        $receipt_no = $request['receipt_no'] ?? '';
        if ($receipt_no != '') {
            $query->where('receipt_no', 'like', '%' . $receipt_no . '%');
        }
        $subject_id = $request['subject_id'] ?? 0;
        if ($subject_id >= -5 && $subject_id!=0) {
            $query->where('subject_id', $subject_id);
        }

        $query->orderBy('create_at', 'desc');

        $isgetall = $request['isgetall'] ?? 0;
        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return Common::paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }
    }

    public function paytypeinfo(){
        return $this->hasOne(PaytypeList::class,'id','pay_type_id');
    }

    public function subjectinfo(){
        return $this->hasOne(Subject::class,'id','subject_id');
    }
}

